package GoodsService

import (
	"context"
	"gdshop-font-api-go/app/request/GoodsReq"
	"gdshop-font-api-go/app/service/GoodsBaseService"
	"gdshop-font-api-go/library/response"
	toolsDb "gdshop-font-api-go/library/tools/db"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
)

func List(ctx context.Context, parames *GoodsReq.List) *response.JsonResponse {
	where := g.Map{
		"g.delete_at < ?": 1,
	}

	if parames.ClassifyId > 0 {
		where["g.cates"] = "," + gconv.String(parames.ClassifyId) + ","
	}

	if len(parames.Keyword) > 0 {
		where["g.goods_name LIKE ?"] = "%" + parames.Keyword + "%"
		// 已搜索关键词，写入历史
		toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "member_search").InsertAndGetId(g.Map{
			"member_id": parames.MemberId,
			"keyword":   parames.Keyword,
		})
	}

	jieguo, err := GoodsBaseService.GetPager(ctx, where, parames.Page, parames.PageSize, parames.Sort)
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}

	return response.SuccessByRequestMessageData(nil, "成功", jieguo)
}
